﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;
using XnaCanvas;

namespace Test.Scenes {
    public partial class XnaModelTest1 : UserControl {
        public XnaModelTest1() {
            InitializeComponent();
            Loaded += XnaModelTest1_Loaded;
        }
        XSence xs1, xs2; XModel xm1, xm2; XObj3 xo1,xo2;
        void XnaModelTest1_Loaded(object sender, RoutedEventArgs e) {
            xs1 = xc1.Children[xc1.Trigger.Csn];
            xs2 = xc2.Children[xc2.Trigger.Csn];
            xs2.RefreshState += xs2_RefreshState;
            xs1.InitialCamera();
            xo1 = new XObj3();
            xo2 = new XObj3();
            xm1 = new XModel();
            xm1.Loaded += xm1_Loaded;
            xm1.ModelIndexSetting += xm1_ModelIndexSetting;
            xo2.Loaded += xo2_Loaded;
            xo1.SetColor(0, 180, 100, 200);
            //xo2.SetColor(33, 180, 100, 240);
            xs1.Add(xo1);
            xs2.Add(xm1);
            xo1.LoadCubePrimitive(size:1);
            this.xc1.Begin();
            this.xc2.Begin();
        }

        void xm1_ModelIndexSetting(XModel obj) {

        }
        void xm1_Loaded(object sender, EventArgs e) {
            //画两个三角，然后连起来组成一个模型
            var xo21 = new XObj2();
            xo21.LoadPolygon();
            var xo22 = new XObj2();
            xo22.LoadPolygon();
            var xm = sender as XModel;
            xm.AddParts(xo21, "up");
            xm.AddParts(xo22, "down");

        }
        void xs2_RefreshState(object sender, RefreshStateArgs e) {            
        }
        void xo2_Loaded(object sender, EventArgs e) {
            //三个法向三个顶点的 三角形
            var r = 1.0f;
            var r2 = -(float)(Math.Sin(MathHelper.Pi / 6) * r);
            var r3 = (float)(Math.Cos(MathHelper.Pi / 6) * r);
            var v1 = new Vector3(r, 0, 0);
            var v2 = new Vector3(r2, r3, 0);
            var v3 = new Vector3(r2, -r3, 0);
            var normal=new Vector3(0,0,1f);
            var color = new Vector4(25, 125, 225, 255);
            var xobj3 = sender as XObj3;
            xobj3.AddVertex(v1, v1,color);
            xobj3.AddVertex(v2, v2,color);
            xobj3.AddVertex(v3, v3,color);
            xobj3.AddIndex3D(0, 1, 2);
            xobj3.AddIndex3D(2, 1, 0);
        }
    }
}
